<template>
  <div>
    <FormPanel ref="registerForm" :height="500" :isSubmitBtn="false" :list="formList" :cols="1" @formChange="formChangeHandle"></FormPanel>
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button type="primary" @click="saveDialog">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import moment from 'moment';
import { SaveInvoiceRegister, queryTax } from '@/api/finance/partFinance/invoiceRegister';
export default {
  name: 'registerFormPanel',
  props: {
    resData: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      saveData: [],
      formList: this.createFormList()
    };
  },
  computed: {},
  watch: {},
  created() { },
  mounted() {
    this.countTax(); // 税率
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '发票号码',
          fieldName: 'CRDENCE'
        },
        {
          type: 'INPUT',
          label: '发票税率',
          fieldName: 'PAY_OFF_TAX',
        },
        {
          type: 'DATE',
          label: '登记日期',
          fieldName: 'PAY_OFF_DATE',
          initialValue: moment(new Date()).format('YYYY-MM-DD'),
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'INPUT',
          label: '登记备注',
          fieldName: 'PAY_OFF_AMOUNT'
        }
      ];
    },
    closeDialog() {
      this.$emit('close', false);
    },
    // 计算税率
    async countTax() {
      const res = await queryTax()
      this.formList.map(x => {
        if (x.fieldName === 'PAY_OFF_TAX') {
          x.initialValue = res.data;
          return;
        } else {
          return;
        }
      });
    },
    formChangeHandle(val) {
      this.saveData = val;
      this.saveData = Object.assign({}, val, this.resData[0])
      this.SaveInvoiceRegister(this.saveData)
    },
    async SaveInvoiceRegister(val) {
      const res = await SaveInvoiceRegister(val)
      console.log(res.data)
      if (res.data == 1) {
        this.$notify.success({ title: '提示信息', message: '发票登记成功!' });
        this.$emit('reAdd');
      } else {
        this.$notify.error({ title: '提示信息', message: '发票登记出错!' });
        this.$emit('reAdd');
      }
    },
    // 点击确定
    saveDialog() {
      if (this.formList.find(x => x.fieldName == 'CRDENCE').initialValue == undefined || this.formList.find(x => x.fieldName == 'CRDENCE').initialValue == '') {
        this.$message.warning('发票编号必填');
        return;
      }
      this.$refs.registerForm.SUBMIT_FORM();
    }
  }
};
</script>
<style lang="less" scoped></style>
